package com.laiketui.order.mapstruct;

import com.laiketui.order.api.dto.OrderParentDTO;
import com.laiketui.order.domain.OrderParent;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Mapper
public interface OrderParentMapstructMapper {
    OrderParentMapstructMapper INSTANCE = Mappers.getMapper(OrderParentMapstructMapper.class);

    @Mapping( target = "totalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderParent.getTotalPrice()))")
    @Mapping( target = "totalCostPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderParent.getTotalCostPrice()))")
    @Mapping( target = "oldTotalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderParent.getOldTotalPrice()))")
    @Mapping( target = "deductionAmount",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderParent.getDeductionAmount()))")
    @Mapping( target = "offlinePayAmount",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderParent.getOfflinePayAmount()))")
    @Mapping( target = "taxation",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderParent.getTaxation()))")
    @Mapping( target = "payPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderParent.getPayPrice()))")
    @Mapping( target = "settlementAmount",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderParent.getSettlementAmount()))")
    OrderParentDTO toDTO(OrderParent orderParent);


   default List<OrderParentDTO> toDTOs(List<OrderParent> orderParents){
       if(CollectionUtils.isEmpty(orderParents)){
           return new ArrayList<>();
       }
       return orderParents.stream().map(this::toDTO).collect(Collectors.toList());
   }
}
